package indi.kider.server.employee.api;

import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.EmployeePart;
import indi.kider.common.model.employee.TenantQuery;
import indi.kider.server.employee.service.EmployeePartService;
import indi.kider.server.employee.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by sq on 2018/7/19.
 */
@RestController
@RequestMapping("employeePart")
public class EmployeePartController {
    @Autowired
    private EmployeePartService employeePartService;
    @Autowired
    private AuthcService authcService;

    /**
     * 保存合作员工和房间号分配关系
     * @param employeePart
     * @return
     */
    @PutMapping("/saveEmployeePart")
    public CommonResponse saveEmployeePart(@RequestBody EmployeePart employeePart) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            return CommonResponse.buildUpdateSuccessResponse(employeePartService.saveEmployeePart(employeePart,employee.getId()),null);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 清除分配关系
     * @param employeePart
     * @return
     */
    @DeleteMapping("/delEmployeePart")
    public CommonResponse delEmployeePart(@RequestBody EmployeePart employeePart) {
        try {
            return CommonResponse.buildUpdateSuccessResponse(employeePartService.delEmployeePart(employeePart),null);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {POST} getTenantPartList
     * @apiGroup employeePart
     * @apiDescription 根据合作企业租客列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employeePart/getTenantPartList
     * {"num":"0","size":"8","query":{TenantQuery}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{}}
     */
    @PostMapping("getTenantPartList")
    public CommonResponse<PageResponse<List<Map>>> getTenantPartList(@RequestBody PageRequest<TenantQuery> request) {
        Employee employee = authcService.getCurrentUser().getData();
        return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse(employeePartService.getTenantPartList(request,employee)));
    }

    /**
     * @api {POST} getPartHouseList
     * @apiGroup employeePart
     * @apiDescription 根据合作企业房源列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employeePart/getPartHouseList
     * {"num":"0","size":"8","query":{TenantQuery}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{}}
     */
    @PostMapping("getPartHouseList")
    public CommonResponse<PageResponse<List<Map>>> getPartHouseList(@RequestBody PageRequest<TenantQuery> request) {
        Employee employee = authcService.getCurrentUser().getData();
        return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse(employeePartService.getPartHouseList(request,employee)));
    }

    /**
     * @api {POST} /getEmployeePartByHouseId
     * @apiGroup employeePart
     * @apiDescription 根据房间ID获取 合作企业管理员
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/employeePart/getEmployeePartByHouseId
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"enterpriseName":"威星","apartmentBrand":"小机器","apartmentType":"公寓"}]}
     * */
    @GetMapping("/getEmployeePartByHouseId/{houseId}")
    public CommonResponse getEmployeePartByHouseId(@PathVariable Long houseId) {
        try {
            return CommonResponse.buildGetSuccessResponse(employeePartService.getEmployeePartByHouseId(houseId));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

}
